


--use PolePosition

-- // checksum failed custom error
--exec sp_addmessage @msgnum = 505050, @severity = 16, @msgtext = N'%s(%s) checksum compare failed', @replace = 'REPLACE', @lang = 'us_english'
GO

-- // not found custom error
--exec sp_addmessage @msgnum = 505060, @severity = 16, @msgtext = N'%s(%s) does not exist', @replace = 'REPLACE', @lang = 'us_english'
GO

-- // unit test custom error
--exec sp_addmessage @msgnum = 505070, @severity = 16, @msgtext = N'%s Test Failed. Expected< %s > Actual< %s > %s', @replace = 'REPLACE', @lang = 'us_english'
GO



--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'v_tb_Vendor_Distribution_Images') and OBJECTPROPERTY(id, N'IsProcedure') = 0)
drop view v_tb_Vendor_Distribution_Images
GO

create view v_tb_Vendor_Distribution_Images as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Selected record(s) from the table; this view is the source for all procedures' select statements
	*/
	
select
	 [tb_Vendor_Distribution_Images_ImageId] as [tb_Vendor_Distribution_Images_ImageId]
	,[filename] as [filename]
	,[Vendor_Distribution_PoleFk] as [Vendor_Distribution_PoleFk]
	-- customization may be required for FriendlyName
	,Left(CONVERT(varchar(255), [Filename]), 255) as [FriendlyName]
from
	tb_Vendor_Distribution_Images
GO





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorDistributionImage_Test') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorDistributionImage_Test
GO


create proc up_VendorDistributionImage_Test
	@tb_Vendor_Distribution_Images_ImageId int
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Test if a record exists, using the primary key
	*/

SET NOCOUNT ON	
	
if exists(
	select [Tb_Vendor_Distribution_Images_ImageId] 
	from [tb_Vendor_Distribution_Images] source
	where [source].[Tb_Vendor_Distribution_Images_ImageId] = @Tb_Vendor_Distribution_Images_ImageId)
begin
	return(1)
end
else
begin
	return(0)
end
GO






--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorDistributionImage_Select') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorDistributionImage_Select
GO

create proc up_VendorDistributionImage_Select
	 @tb_Vendor_Distribution_Images_ImageId int = null 
	,@filename nvarchar(100) = null 
	,@Vendor_Distribution_PoleFk int = null 
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Selected record(s) based on the passed arguments; no arguments returns all
	*/

SET NOCOUNT ON	

select * from v_tb_Vendor_Distribution_Images
where
		IsNull([v_tb_Vendor_Distribution_Images].[Tb_Vendor_Distribution_Images_ImageId], -1) = Coalesce(@Tb_Vendor_Distribution_Images_ImageId, [v_tb_Vendor_Distribution_Images].[Tb_Vendor_Distribution_Images_ImageId], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Images].[Filename], '') = Coalesce(@Filename, [v_tb_Vendor_Distribution_Images].[Filename], '')
	AND	IsNull([v_tb_Vendor_Distribution_Images].[Vendor_Distribution_PoleFk], -1) = Coalesce(@Vendor_Distribution_PoleFk, [v_tb_Vendor_Distribution_Images].[Vendor_Distribution_PoleFk], -1)
GO





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorDistributionImage_Select_Top1') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorDistributionImage_Select_Top1
GO

create proc up_VendorDistributionImage_Select_Top1
	 @tb_Vendor_Distribution_Images_ImageId int = null 
	,@filename nvarchar(100) = null 
	,@Vendor_Distribution_PoleFk int = null 
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Selected top 1 record based on the passed arguments; no arguments returns all
	*/

SET NOCOUNT ON	

select TOP 1 * from v_tb_Vendor_Distribution_Images
where
		IsNull([v_tb_Vendor_Distribution_Images].[Tb_Vendor_Distribution_Images_ImageId], -1) = Coalesce(@Tb_Vendor_Distribution_Images_ImageId, [v_tb_Vendor_Distribution_Images].[Tb_Vendor_Distribution_Images_ImageId], -1)
	AND	IsNull([v_tb_Vendor_Distribution_Images].[Filename], '') = Coalesce(@Filename, [v_tb_Vendor_Distribution_Images].[Filename], '')
	AND	IsNull([v_tb_Vendor_Distribution_Images].[Vendor_Distribution_PoleFk], -1) = Coalesce(@Vendor_Distribution_PoleFk, [v_tb_Vendor_Distribution_Images].[Vendor_Distribution_PoleFk], -1)
GO




--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorDistributionImage_Insert') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorDistributionImage_Insert
GO

create proc up_VendorDistributionImage_Insert
	 @tb_Vendor_Distribution_Images_ImageId int =null OUTPUT  
	,@filename nvarchar(100) = null  
	,@Vendor_Distribution_PoleFk int = null  
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Inserts a new record; missing arguments are set as their default (typically null)
	*/

SET NOCOUNT ON	
--int

insert tb_Vendor_Distribution_Images 
	(
	
	 [filename] 
	,[Vendor_Distribution_PoleFk] 
	)
	values
	(
	
	 @filename 
	,@Vendor_Distribution_PoleFk 
	)
set @Tb_Vendor_Distribution_Images_ImageId = scope_identity()
select * from v_tb_Vendor_Distribution_Images where Tb_Vendor_Distribution_Images_ImageId = @Tb_Vendor_Distribution_Images_ImageId

GO




--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorDistributionImage_Update') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorDistributionImage_Update
GO


create proc up_VendorDistributionImage_Update
	 @tb_Vendor_Distribution_Images_ImageId int  
	,@filename nvarchar(100) = null  
	,@Vendor_Distribution_PoleFk int = null  
	,@OriginalTb_Vendor_Distribution_Images_ImageId int  = null --Original PrimaryKey
	
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Updates an existing record, verifying exist; missing arguments persist original record value
		NOTE: this version of the update allows up to write nulls to the table.
	*/

SET NOCOUNT ON	

if @OriginalTb_Vendor_Distribution_Images_ImageId is null
	set @OriginalTb_Vendor_Distribution_Images_ImageId = @Tb_Vendor_Distribution_Images_ImageId

-- test
declare @Result bit
execute @Result = up_VendorDistributionImage_Test @OriginalTb_Vendor_Distribution_Images_ImageId
if (not @Result = 1)
begin
	Declare @x varchar(100)
	Set @x = Convert(Varchar(100), @tb_Vendor_Distribution_Images_ImageId)
	raiserror(505060, 16, 1, 'VendorDistributionImage', @x)
	return
end

-- proceed
update tb_Vendor_Distribution_Images 
set

	 [filename] =  @filename
	,[Vendor_Distribution_PoleFk] =  @Vendor_Distribution_PoleFk
	
from tb_Vendor_Distribution_Images source
where
	[source].[Tb_Vendor_Distribution_Images_ImageId] = @OriginalTb_Vendor_Distribution_Images_ImageId


select * from v_tb_Vendor_Distribution_Images where Tb_Vendor_Distribution_Images_ImageId = @Tb_Vendor_Distribution_Images_ImageId
GO





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorDistributionImage_Delete') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorDistributionImage_Delete
GO


create proc up_VendorDistributionImage_Delete
	@tb_Vendor_Distribution_Images_ImageId int
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Deletes an existing record, verifying exist
	*/

SET NOCOUNT ON	

-- test
declare @Result bit
execute @Result = up_VendorDistributionImage_Test @tb_Vendor_Distribution_Images_ImageId
if (not @Result = 1)
begin
	Declare @x varchar(100)
	Set @x = Convert(Varchar(100), @tb_Vendor_Distribution_Images_ImageId)
	raiserror(505060, 16, 1, 'VendorDistributionImage', @x)
	return
end

-- proceed
delete from tb_Vendor_Distribution_Images 
where [Tb_Vendor_Distribution_Images_ImageId] = @Tb_Vendor_Distribution_Images_ImageId

GO


